﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using DomainApplication;
using DomainApplication.Models;

namespace ApplicationDAL
{
    public class ApplicationRepository : IApplicationRepository
    {

        public List<DesignPattern> GetDesignPatterns()
        {
            List<DesignPattern> list = new List<DesignPattern>();

            using (var ctx = new ApplicationServiceContext())
            {
                var results = ctx.DesignPatterns.AsEnumerable();

                foreach (var result in results)
                {
                    list.Add(new DesignPattern()
                    {
                        Name = result.Name
                    });
                }
            }

            return list;

        }


        public DesignPattern GetDesignPattern(int Id)
        {
            using (var ctx = new ApplicationServiceContext())
            {
                var result = ctx.DesignPatterns.SingleOrDefault(p => p.DesignPatternId == Id);

                return new DomainApplication.Models.DesignPattern()
                {
                    Name = result.Name
                };
            }

        }

        public DesignPattern GetDesignPattern(string name)
        {
            throw new NotImplementedException();
        }

        public DesignPattern AddDesignPattern(DesignPattern newItem)
        {
            using (var ctx = new ApplicationServiceContext())
            {
                var newPattern = new Models.DesignPattern()
                {
                    Name = newItem.Name,
                    Summary = String.Format("Summary for {0}", newItem.Name)
                };
                newPattern = ctx.DesignPatterns.Add(newPattern);
                ctx.SaveChanges();
            }
            return newItem;
        }

    }
}
